package cz.fit.vutbr.pis2012.augeaswh.managers;

import java.math.BigDecimal;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import cz.fit.vutbr.pis2012.augeaswh.persistence.SlotsStockItem;
import cz.fit.vutbr.pis2012.augeaswh.persistence.StockItem;
import cz.fit.vutbr.pis2012.augeaswh.persistence.StockType;

@Stateless
public class StockTypesManager {
	@PersistenceContext
	private EntityManager em;
	
	private int amount;
	
	@SuppressWarnings("unchecked")
	public List<StockType> getStockTypes() {
		return	em.createQuery("SELECT st FROM StockType st").getResultList();
	}
	
	public void save(StockType stockType) {
		em.merge(stockType);			
	}
	
	public void delete(StockType stoctType) {
	    em.remove(em.merge(stoctType));
	}
	
	public StockType getStockTypeById(Long id) {
		@SuppressWarnings("unchecked")
		List<StockType> list =  em.createQuery("SELECT st FROM StockType st WHERE st.id = :id").setParameter("id", id).getResultList();
		return (list.size() > 0) ? list.get(0) : null;
	}
	
	public int getAmount(StockType stockType)
	{
		amount = 0;
		
		for (StockItem stockItem : stockType.getStockItems()) 
		{
			for (SlotsStockItem item : stockItem.getSlotsStockItems()) 
			{
				amount = amount + item.getAmmount().intValue();
			}			
		}			
		return amount;
	}
}
